﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class UIFieldData
    {
        public float MaxHp
        {
            get { return 1f; }
        }
        #region Anger
        private float anger = 0f;
        public float Anger
        {
            set
            {
                this.anger = value;
            }
            get
            {
                return anger;
            }
        }
        #endregion

        #region current hp
        private float hp;
        public float currentHp
        {
            get
            {
                return (float)hp;
            }
            set
            {
                hp = value;
            }

        }
        #endregion


        public int kills;
        public int beKills;

        public PlayerSide ps;
        private string refreshDate;
        
        public string RefreshDate
        {
            set
            {
                if(!string.IsNullOrEmpty(refreshDate) &&
                    !string.IsNullOrEmpty(value) &&
                    value != refreshDate)
                {
                    Debug.Log("[DEBUG Log current refresh date] " + value+" preview date:"+refreshDate);
                }

                refreshDate = value;
            }
        }

        float[] heroHps = new float[4] { 1, 1, 1, 1 };

        public void ResetData(PlayerSide ps)
        {
            this.ps = ps;
            this.beKills = ps.killeds;
            this.kills = ps.kills;
            
            StaticData.playerData.Tiredness = ps.tiredness;

            this.RefreshDate = ps.tiredNessLastRefreshDate;
            
            Anger = (float)ps.anger;
            hp = (float)ps.curHp;

            heroHps[0] = (float)ps.battlePos3;
            heroHps[1] = (float)ps.battlePos7;
            heroHps[2] = (float)ps.battlePos9;
            heroHps[3] = (float)ps.battlePos13;
        }

        public void AddHpPercent(float per,out bool succ)
        {
            if (hp < 1.0f)
                succ = true;
            else
                succ = false;

            int count = 0;
            foreach(var var in StaticData.playerData.InstPlayerData.BattleCamps)
            {
                if (var.Value != null && var.Value.hero != null && var.Value.hero.heroData != null) count++;
            }
            

            float[] temp = new float[count];

            for(int i=0; i< count; i++)
            {
                temp[i] = heroHps[i];
                heroHps[i] += per;
                heroHps[i] = Mathf.Clamp01(heroHps[i]);
            }

            float avg = 0f;
            for (int j= 0;j < count; j++)
            {
                avg += heroHps[j]-temp[j];
            }

            var willAdd = avg / count;
            hp +=  willAdd;
            if (willAdd <= 0f && hp>0.99999f)
                hp = 1f;
            hp = Mathf.Clamp01(hp);    
        }
      
        public bool playerInField = false;
        public static bool InField(long playerId)
        {
            if(playerId == StaticData.playerData.playerId)
            {
                return StaticData.fieldData.playerInField;
            }
            return StaticData.playerInfos.ContainsKey(playerId);
        }
    }
}